<?php
/**
 * @file name : <MODEL_NAME>_model.php
 * @package   : <PROJ_NAME>
 * @author    : Vo Thanh Minh
 * @email     : <thanhminhld@yahoo.com - thanhminhld@gmail.com>
 * @copyright : <CPR>
 *
 */
if (!defined('BASEPATH')) {
    exit('No direct script access');
}

class <MODEL_CLSS_NAME>_model extends Base_Model
{
    /**
     * @function __construct
     * @author   Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     */
    public function __construct()
    {
        parent::__construct();
        $this->_tbl        = '<PRI_TBL_NAME>';
        $this->_primary_id = '<PRI_TBL_ID>';
    }
    /**
     * getTotalFields
     *
     * @author Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     * @access public
     * @param  string
     *
     * @return int
     */
    public function getTotalFields($field = '*')
    {
        return $this->db->count_field_results($this->_tbl, $field);
    }
    /**
     * @function loadListIn
     * @author      Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     * @access      public
     * @param       string
     * @param       array
     * @param     string
     * @param     string
     * @return     array
     */
    public function loadListIn($field, $arrWhereIn = array(), $order = '', $mode = '', $arrWhere = array())
    {
        if (! is_string($field) || strlen($field) <= 0) {
            return NULL;
        }
        if (count($arrWhereIn) <= 0) {
            return NULL;
        }

        $this->db->where_in($field, $arrWhereIn);
        if (count($arrWhere) > 0) {
            $this->db->where($arrWhere);
        }
        if (is_string($order) && strlen($order) > 0) {
            if ( ! preg_match('/(asc|ASC|desc|DESC)/', $mode)) {
                $mode = 'asc';
            }
            $this->db->order_by($order, $mode);
        }

        $query = $this->db->get($this->_tbl);
        $arrList = array();
        if ($query->num_rows() > 0) {
            foreach($query->result() as $row) {
                $arrList[] = $row;
            }
        }
        return $arrList;
    }
    /**
     * @function loadRecord
     *
     * By default this function will return array of object.
     * If you want only one object so $objRet variable is TRUE
     *
     * @author Vo Thanh Minh <vothanhminh@kng-vn.com>
     * @access public
     * @param  int
     * @return array
     */
    public function loadRecord($pri_id, $lang_id = '', $objRet = FALSE)
    {
        if (is_numeric($lang_id) && $lang_id > 0) {
            $where = array($this->_primary_id  => $pri_id,
                           'lang_id'           => $lang_id);
        } else {
            $where = array($this->_primary_id  => $pri_id);
        }

        $itemObj = $this->db->get_where($this->_tbl, $where);
        $arrList = array();
        if ($itemObj->num_rows() > 0) {
            if ($objRet == TRUE) {
                foreach($itemObj->result() as $row) {
                   $arrList = $row;
                   break;
                }
            } else {
                foreach($itemObj->result() as $row) {
                   $arrList[] = $row;
                }
            }
        }
        return $arrList;
    }
}
